<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws.
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.).
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations).
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/

/* $File: ./include/modules/Bulletin/classes/PhpFox_ComponentCompose.class.php, $Author:  $ */



/** Post bulletin
 *
 *
 * @package module.bulletin
 * @version $Id:
 *   PHPFox Team ()
 */
class PhpFox_Mod_Bulletin_ComponentCompose extends BaseComponent
{
	function process()
	{
        App::loadClass('Text');
        $oTxt = &new Text();

	    $oReq = &App::getRequest();
	    $oSrvBulletin = $this->_oModule->getService('Bulletin');
	    $oSrvAccount = &App::getModuleService('Account', 'Security');
	    $oUser = $oSrvAccount->getCurrentUser();
	    $sCurrentUser = $oSrvAccount->getCurrentUserLogin();
	    $bIsAdmin = ($oSrvAccount->isAdmin() || $oSrvAccount->isStaff()) ? true : false;

	    $bPreview = false;

	    $aFields = array(
			'aMess[title]'   => array('title'=>App::format('Mod_Bulletin.subject'), 'def'=>'required'),
        );

        $oValidator = &App::newObject('Validator', $aFields);

        $aPost = $oReq->getArray('aMess');
        if (isset($aPost['text']))
        {
			$aPost['text'] = $oTxt->parseText($aPost['text'],0,0,1);
        }
		
        if($oReq->getInt('id'))
        {
	        $aBulletinInfo = $oSrvBulletin->getBulletinById($oReq->getInt('id'));
            if(!$bIsAdmin && ($aBulletinInfo['user'] != $sCurrentUser))
                App::gotoUrl('public.account.bulletin');
        }
	    else
	       $aBulletinInfo = $aPost;
	    $sPreviewTxt = '';
	    switch ($oReq->getAction())
	    {
	    	case 'sPrev':
	    		if($oValidator->isValid($oReq->getAll()))
	    	    {
	    	        $bPreview = true;
	    	        $sPreviewTxt = $aPost['text'];
	    	        $aBulletinInfo = $aPost;
	    	    }
	    	    else
	    	      $this->addErrors($oValidator->getErrors());
	    		break;

	    	case 'sSend':
	    	    if($oValidator->isValid($oReq->getAll()))
	    	    {
    	    	    if(empty($aPost['text']))
    	    	        $this->addErrors(App::format('Mod_Bulletin.text.error'));
    	    	    $aErrors = $this->getErrors();
    	    	    if(empty($aErrors))
    	    	    {
		    	        if ($oReq->getInt('id'))
	    	    	    {
	    	    	      if($bIsAdmin || ($sCurrentUser == $oSrvBulletin->getBulletinById($oReq->getInt('id'), 'user')))
	    	    	          $oSrvBulletin->updateBulletin($oReq->getInt('id'),$aPost);
	    	    	      else
	    	    	          App::gotoUrl('public.account.bulletin');
	    	    	    }
	    	    	    else
	    	    	      $oSrvBulletin->addBulletin($aPost, $sCurrentUser);

	    	    	    App::gotoUrl('public.account.bulletin', array(), App::format('Mod_Bulletin.item.submited'), false, 'Board');
    	    	    }
    	    	    else
    	    	        $aBulletinInfo = $aPost;
	    	    }
	    	    else
	    	      $this->addErrors($oValidator->getErrors());
	    		break;
	    }

	    $oTpl = &$this->_oModule->getTemplate();

        $oTpl->assignSrc(array(
                                'jsValidation'  => $oValidator->makeJS(),
		                        'jsValMess'     => $oValidator->makeJsMess(),
		                        'aErrors'		=> $this->getErrors(),
		                        'sPreviewTxt'   => $sPreviewTxt,
		                        'aMess'        => $aBulletinInfo,
                                )
        );
        $oTpl->assign(array(
			                 'sBoxName'     => App::format('bb.board'),
			                 'bPreview'     => $bPreview,
			));

		$srcHtml = $oTpl->fetch('Compose.html');
		return $srcHtml;
	}
}
?>